package com.qkbb.admin.kuibu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.qkbb.JavaBean.Game;
import com.qkbb.admin.kuibu.qkbb.funcation.ImageDownloadSDCardCacheHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.OSShelp;

import java.util.List;

/**
 * Created by admin on 2016/2/4.
 */
public class ViewPagerListAdapter extends BaseAdapter {
    private List<Game> listbody;
    private List<Game> listhead;
    private OSShelp osShelp;
    private Bitmap bitmap;
    private int postions;
    private Handler handler;
    private Context context;
    private String diskCachePath;
    private ImageDownloadSDCardCacheHelper imageDownloadSDCardCacheHelper;
    public ViewPagerListAdapter(List<Game> listbody, List<Game> listhead,Context context) {
        this.listbody = listbody;
        this.listhead = listhead;
        this.context=context;
    }

    @Override
    public int getCount() {
        return listbody.size();
    }

    @Override
    public Object getItem(int position) {
        return listbody.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    PagerViewHold viewhold;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        osShelp=new OSShelp(context);
        if (convertView==null){
            viewhold=new PagerViewHold();
            convertView= LayoutInflater.from(context).inflate(R.layout.viewpager_list_item,null);
            viewhold.imageView= (ImageView) convertView.findViewById(R.id.viewpager_item_imageview);
            viewhold.peopleview= (TextView) convertView.findViewById(R.id.viewpager_item_renshu);
            viewhold.timeview= (TextView) convertView.findViewById(R.id.viewpager_item_time);
            viewhold.titleview= (TextView) convertView.findViewById(R.id.viewpager_item_title);
            viewhold.scheduleview= (TextView) convertView.findViewById(R.id.viewpager_item_jindu);
            viewhold.descriptionview= (TextView) convertView.findViewById( R.id.viewpager_item_text_description);
            convertView.setTag(viewhold);
        }
        else{
            viewhold= (PagerViewHold) convertView.getTag();
        }
            viewhold.titleview.setText(listbody.get(position).getRoadname());
        Log.e("getRoadname()", listbody.get(position).getRoadname());
            viewhold.descriptionview.setText(listbody.get(position).getDescription());
            viewhold.peopleview.setText(listbody.get(position).getNumber_of_participants() + "");
        if (!listbody.get(position).getStarttime().equals("null")){
            viewhold.timeview.setText(listbody.get(position).getStarttime());
        }

        setImage(viewhold.imageView, listbody.get(position).getPictureurl());
        if (listbody.get(position).getRoadsteplength() != 0) {
            viewhold.scheduleview.setText(listbody.get(position).getTotal_steps()/listbody.get(position).getRoadsteplength()+"");
        }




        return convertView;
    }
    public void setImage(final ImageView view,String imagename){
//        String uri=osShelp.getBitmap(imagename);
//        Picasso.with(context).load(uri).into(view);
        String uri=osShelp.getBitmap(imagename);
        imageDownloadSDCardCacheHelper=new ImageDownloadSDCardCacheHelper();
        imageDownloadSDCardCacheHelper.myDownloadImageSDCardCache(context, uri, view, imagename);
//        RequestQueue queue= Volley.newRequestQueue(context);
//        ImageLoader imageLoader=new ImageLoader(queue,MyImageCache.getMyImageCache());
//        ImageLoader.ImageListener imageListener=ImageLoader.getImageListener(view,R.drawable.abc_cab_background_top_mtrl_alpha,R.drawable.abc_popup_background_mtrl_mult);
//        imageLoader.get(uri,imageListener);
    }
    class PagerViewHold{
        ImageView imageView;
        TextView titleview;
        TextView timeview;
        TextView peopleview;
        TextView scheduleview;
        TextView descriptionview;
    }

}
